#include "processor.h"
#include "parser.h"

typedef std::bitset<16> vmc_register;
typedef unsigned long int ul_int;

Processor::Processor()
{
  for(unsigned long i = 0; i != 65535; i++) {
    memory[i] = vmc_register();
  }
}

Processor::~Processor()
{

}

bool Processor::setRegister(ul_int adress, vmc_register value)
{
  if (memory.find(adress) != memory.end())
  {
    memory[adress] = value;
    return true;
  }
  else
  {
    return false;
  }
}

vmc_register Processor::getRegister(ul_int adress)
{
  return memory[adress];
}

void Processor::push(vmc_register value) {
  pstack.push(value);
}

vmc_register Processor::pop() {
  bstemp = pstack.top();
  pstack.pop();
  return bstemp;
}

void Processor::execute(std::string programFile) {
  //boost::shared_ptr<Processor> t;
  boost::shared_ptr<Parser> p(new Parser(programFile.c_str(), this));
  runCode();
}

void Processor::addCommand(boost::shared_ptr<Command> command) {
  code.push_back(command);
}

void Processor::runCode() {
  std::vector<boost::shared_ptr<Command> >::iterator i;
  for(i = code.begin(); i != code.end(); i++) {
    std::cout << "exec instruction: " << (*i)->getInstruction() << std::endl;
  }
  code.clear();
}

